//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CallableBondVolatilityStructure.h"
using namespace Cephei::QL::Experimental::Callablebonds;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/TermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (boost::shared_ptr<QuantLib::CallableBondVolatilityStructure>& childNative, Object^ owner) : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCallableBondVolatilityStructure = NULL;
#endif
	_ppCallableBondVolatilityStructure = &childNative;
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
}
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (QuantLib::CallableBondVolatilityStructure& childNative, Object^ owner) : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCallableBondVolatilityStructure = NULL;
#endif
	_ppCallableBondVolatilityStructure = new boost::shared_ptr<QuantLib::CallableBondVolatilityStructure> (&childNative);
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
    _CallableBondVolatilityStructureOwner = owner;
    _TermStructureOwner = owner;
}

Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (CCallableBondVolatilityStructure^ copy) : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCallableBondVolatilityStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCallableBondVolatilityStructure = new boost::shared_ptr<QuantLib::CallableBondVolatilityStructure> (copy->GetShared());
        _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (PLATFORM::Type^ t) : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCallableBondVolatilityStructure = NULL;
#endif
	if (!t->IsSubclassOf(CCallableBondVolatilityStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (QuantLib::Handle<QuantLib::CallableBondVolatilityStructure>& childNative, Object^ owner)  : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
	_phCallableBondVolatilityStructure = &childNative;
	_ppCallableBondVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::CallableBondVolatilityStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
    _CallableBondVolatilityStructureOwner = owner;
}
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (QuantLib::Handle<QuantLib::CallableBondVolatilityStructure> childNative)  : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
	_phCallableBondVolatilityStructure = &childNative;
	_ppCallableBondVolatilityStructure = &static_cast<boost::shared_ptr<QuantLib::CallableBondVolatilityStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::CCallableBondVolatilityStructure (QuantLib::CallableBondVolatilityStructure childNative)  : CTermStructure(CCallableBondVolatilityStructure::typeid)
{
#ifdef HANDLE
	_phCallableBondVolatilityStructure = NULL;
#endif
	_ppCallableBondVolatilityStructure = new boost::shared_ptr<QuantLib::CallableBondVolatilityStructure> (new QuantLib::CallableBondVolatilityStructure (childNative));
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppCallableBondVolatilityStructure));
}
#endif

Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::~CCallableBondVolatilityStructure ()
{
    if (_ppCallableBondVolatilityStructure != NULL)
    {
	    delete _ppCallableBondVolatilityStructure;
        _ppCallableBondVolatilityStructure = NULL;
    }
}
Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::!CCallableBondVolatilityStructure ()
{
    if (_ppCallableBondVolatilityStructure != NULL)
    {
	    delete _ppCallableBondVolatilityStructure;
    }
}
QuantLib::CallableBondVolatilityStructure& Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::GetReference ()
{
    if (_ppCallableBondVolatilityStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppCallableBondVolatilityStructure;
}
boost::shared_ptr<QuantLib::CallableBondVolatilityStructure>& Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::GetShared ()
{
    if (_ppCallableBondVolatilityStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppCallableBondVolatilityStructure;
}
QuantLib::CallableBondVolatilityStructure* Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::GetPointer ()
{
    if (_ppCallableBondVolatilityStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppCallableBondVolatilityStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CallableBondVolatilityStructure>& Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::GetHandle ()
{
	if (_phCallableBondVolatilityStructure == NULL)
	{
		_phCallableBondVolatilityStructure = new Handle<QuantLib::CallableBondVolatilityStructure> (*_ppCallableBondVolatilityStructure);
	}
	return *_phCallableBondVolatilityStructure;
}
#endif
bool Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::HasNative () 
{
	return (_ppCallableBondVolatilityStructure != NULL);
}

Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::BlackVariance (Cephei::QL::Times::IPeriod^ optionTenor, Cephei::QL::Times::IPeriod^ bondTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CbondTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCallableBondVolatilityStructure)->blackVariance ( _optionTenor,  _bondTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::BlackVariance (DateTime optionDate, Cephei::QL::Times::IPeriod^ bondTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CbondTenor;
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate); //a
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCallableBondVolatilityStructure)->blackVariance ( _optionDate,  _bondTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::BlackVariance (Double optionTime, Double bondLength, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime); //a
        QuantLib::Time _bondLength = (QuantLib::Time)ValueHelper::Convert (bondLength); //a
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCallableBondVolatilityStructure)->blackVariance ( _optionTime,  _bondLength,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::BusinessDayConventionEnum Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppCallableBondVolatilityStructure)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::MaxBondLength::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppCallableBondVolatilityStructure)->maxBondLength ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::MaxBondTenor::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppCallableBondVolatilityStructure)->maxBondTenor ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCallableBondVolatilityStructure)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCallableBondVolatilityStructure)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::OptionDateFromTenor (Cephei::QL::Times::IPeriod^ optionTenor)
{
    CPeriod^ _CoptionTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCallableBondVolatilityStructure)->optionDateFromTenor ( _optionTenor );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::SmileSection (Cephei::QL::Times::IPeriod^ optionTenor, Cephei::QL::Times::IPeriod^ bondTenor)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CbondTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppCallableBondVolatilityStructure)->smileSection ( _optionTenor,  _bondTenor );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::SmileSection (DateTime optionDate, Cephei::QL::Times::IPeriod^ bondTenor)
{
    CPeriod^ _CbondTenor;
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate); //a
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppCallableBondVolatilityStructure)->smileSection ( _optionDate,  _bondTenor );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = REFNEW Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::Volatility (Cephei::QL::Times::IPeriod^ optionTenor, Cephei::QL::Times::IPeriod^ bondTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CbondTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCallableBondVolatilityStructure)->volatility ( _optionTenor,  _bondTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::Volatility (DateTime optionDate, Cephei::QL::Times::IPeriod^ bondTenor, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _CbondTenor;
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate); //a
        _CbondTenor = safe_cast<CPeriod^> (bondTenor);
        _CbondTenor->Lock();
        QuantLib::Period& _bondTenor = static_cast<QuantLib::Period&> (_CbondTenor->GetReference ()); 
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCallableBondVolatilityStructure)->volatility ( _optionDate,  _bondTenor,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CbondTenor != nullptr) _CbondTenor->Unlock();
    }
}
Double Cephei::QL::Experimental::Callablebonds::CCallableBondVolatilityStructure::Volatility (Double optionTime, Double bondLength, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _optionTime = (QuantLib::Time)ValueHelper::Convert (optionTime); //a
        QuantLib::Time _bondLength = (QuantLib::Time)ValueHelper::Convert (bondLength); //a
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppCallableBondVolatilityStructure)->volatility ( _optionTime,  _bondLength,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

